package com.play2gether;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by Raul on 30/03/14.
 */
public class IndiceKamasutraActivity extends ActionBarActivity implements View.OnClickListener {

    private DataBaseManager    manager;
    public GridView            gvPosturas;
    //Para navbar
    public Button btFormularioNV;
    public Button btSugerenciaNV;
    public Button btOpcionesNV;
    public String[] estado;
    public String id;

    public SharedPreferences preferencias;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kamasutra_indice);

        TextView tituloNav = (TextView) findViewById(R.id.tituloLayout);
        tituloNav.setText("Kamasutra");
        //actionBar.setTitulo("Prueba");

        //Clase para la base de datos
        manager = new DataBaseManager(this);


        gvPosturas = (GridView) findViewById(R.id.gvPosturas);
        gvPosturas.setAdapter(new ImageAdapter(this));
        gvPosturas.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                            public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
                                                Intent intent = new Intent(IndiceKamasutraActivity.this,kamasutraActivity.class);
                                                intent.putExtra("id", position+1);
                                                startActivity(intent);
                                            }}
        );

        //Para navbar
        preferencias = getSharedPreferences("sesion", Context.MODE_PRIVATE);
        id = preferencias.getString("id", "");
        //Navbar
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setCustomView(R.layout.navbar);
        //Botones para el navbar
        btFormularioNV = (Button) findViewById(R.id.btFormularioNV);
        btSugerenciaNV = (Button) findViewById(R.id.btSugerenciaNV);
        btOpcionesNV   = (Button) findViewById(R.id.btOpcionesNV);

        estado = utilidades.obtenerEstado(id, this);
        if (!estado[0].equals("2")){
            btFormularioNV.setAlpha(0);
            btSugerenciaNV.setAlpha(0);
        }else{
            btFormularioNV.setOnClickListener(this);
            btSugerenciaNV.setOnClickListener(this);
        }
        btOpcionesNV.setOnClickListener(this);
        //btLogoNV.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;

        switch (v.getId()) {
            case R.id.btFormularioNV:
                Intent intentF = new Intent(IndiceKamasutraActivity.this,indexFormularios.class);
                intentF.putExtra("id",id);
                startActivity(intentF);
                break;
            case R.id.btSugerenciaNV:
                Intent intentS = new Intent(IndiceKamasutraActivity.this,indexSugerencias.class);
                intentS.putExtra("id",id);
                startActivity(intentS);
                break;
            case R.id.btOpcionesNV:
                Toast.makeText(getApplicationContext(), "opciones", Toast.LENGTH_SHORT).show();
                mostrarOpciones();
                break;
        }
    }

    private void mostrarOpciones(){
        final CharSequence[] items2 = {"Mi perfil", "Configurar mi play2gether", "Cerrar sesión", "Desvincularme de mi pareja", "Sobre play2gether", "Cerrar"};

        AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
        builder2.setTitle("Menu de opciones");
        builder2.setItems(items2, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
            dialog.cancel();
            switch(item){
                case 0: Intent intent = new Intent(IndiceKamasutraActivity.this,PerfilActivity.class);
                        startActivity(intent);
                        break;
                case 1: break;
                case 2: guardarSesion("0");
                        intent = new Intent(IndiceKamasutraActivity.this,loginActivity.class);
                        finish();
                        startActivity(intent);
                        break;
                case 3: break;
                case 4: break;
           }
            }
        });
        AlertDialog alert2 = builder2.create();
        alert2.requestWindowFeature(Window.FEATURE_NO_TITLE);
        WindowManager.LayoutParams wmlp2 = alert2.getWindow().getAttributes();
        wmlp2.gravity = Gravity.TOP | Gravity.RIGHT;
        wmlp2.x = 0;   //x position
        wmlp2.y = 124;   //y position
        alert2.show();
    }
    public void guardarSesion(String n){
        preferencias = getSharedPreferences("sesion", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferencias.edit();
        editor.putString("activa", n);
        editor.putString("id","");
        editor.commit();
        finish();
    }
    public void sincronizar(){
        Sincronizar sinc = new Sincronizar(id,this);
        sinc.sincronizar();

    };
}
